<?php

class ZFBootstrap_Grid_Deploy_Table extends Bvb_Grid_Deploy_Table {

	private $_gridName = '';

	protected function _buildExtraRows ($position) {
		$r = parent::_buildExtraRows($position);
		if (($position == 'beforePagination') and ($r != '')) {
			$r = '<tfoot>' . $r . '</tfoot>';
		}

		return $r;
	}

	protected function _buildPagination () {
		return '';
	}

	protected function _buildPaginationAfterTable () {
		$pageSelect = '';

		$url = $this->getUrl(array('start'));

		$actual = (int) $this->getParam('start');

		$ppagina = (int) $this->getParam('perPage');
		if ($ppagina == 0) {
			$ppagina = $this->_recordsPerPage;
		}
		$result2 = '';

		$pa = $actual == 0 ? 1 : ceil($actual / $ppagina) + 1;

		// Calculate the number of pages
		if ($this->_recordsPerPage > 0) {
			$npaginas = ceil($this->_totalRecords / $ppagina);
			$actual = floor($actual / $ppagina) + 1;
		} else {
			$npaginas = 0;
			$actual = 0;
		}

		$pag = '';

		$urli = "$url/start{$this->getGridId()}/0";
		if (1 == $actual) {
			$pag .= "<li class=\"active\"><a href=\"$urli\">1</a></li>";
		} else {
			$pag .= "<li><a href=\"$urli\">1</a></li>";
		}

		//$pag .= ( $actual > 5) ? " ... " : "  ";

		if ($npaginas > 5) {

			$in = min(max(1, $actual - 4), $npaginas - 5);
			$fin = max(min($npaginas, $actual + 4), 6);

			for ($i = $in + 1; $i < $fin; $i++) {

				$urli = $url . "/start{$this->getGridId()}/" . (($i - 1) * $ppagina);
				if ($i == $actual) {
					$pag .= "<li class=\"active\"><a href=\"$urli\">$i</a></li>";
				} else {
					$pag .= "<li><a href=\"$urli\">$i</a></li>";
				}
			}

			$pag .= ( $fin < $npaginas) ? " ... " : "  ";
		} else {

			for ($i = 2; $i < $npaginas; $i++) {

				$urli = $url . "/start{$this->getGridId()}/" . (($i - 1) * $ppagina);
				if ($i == $actual) {
					$pag .= "<li class=\"active\"><a href=\"$urli\">$i</a></li>";
				} else {
					$pag .= "<li><a href=\"$urli\">$i</a></li>";
				}
			}
		}

		$urli = $url . "/start{$this->getGridId()}/" . (($npaginas - 1) * $ppagina);
		if ($actual == $npaginas) {
			$pag .= "<li class=\"active\"><a href=\"$urli\">$npaginas</a></li>";
		} else {
			$pag .= "<li><a href=\"$urli\">$npaginas</a></li>";
		}


		if ($actual != 1) {
			$pag = "<li><a href=\"$url/start/0\">" . $this->__('First') . "</a></li>
				<li><a href=\"$url/start{$this->getGridId()}/" . (($actual - 2) * $ppagina) . "\">" . $this->__('Previous') . "</a></li>"
					. $pag;
		}

		if ($actual != $npaginas) {

			$pag .= "<li><a href=\"$url/start{$this->getGridId()}/" . ($actual * $ppagina) . "\">" . $this->__('Next') . "</a></li>
				<li><a href=\"$url/start{$this->getGridId()}/" . (($npaginas - 1) * $ppagina) . "\">" . $this->__('Last') . "</a></li>";
		}

		$pag = '<div class="pagination pagination-centered"><ul>' . $pag . '</ul></div>';

		$pageSelect = '';

		if ($npaginas > 1 || count($this->_export) > 0) {
			//get actual record
			if ($actual <= 1) {
				$registoActual = 1;
				$registoFinal = $this->_totalRecords > $ppagina ? $ppagina : $this->_totalRecords;
			} else {
				$registoActual = $actual * $ppagina - $ppagina;

				if ($actual * $ppagina > $this->_totalRecords) {
					$registoFinal = $this->_totalRecords;
				} else {
					$registoFinal = $actual * $ppagina;
				}
			}

			$images = $this->_temp['table']->images($this->getImagesUrl());

			$url1 = $url = $this->getUrl(array('start'));

			$this->_render['export'] = $this->_temp['table']->export(
					$this->getExports(), $this->getImagesUrl(), $url1, $this->getGridId()
			);

			if ((int) $this->getInfo("limit") > 0) {

				$result2 = $this->_temp['table']->pagination('', (int) $this->getInfo("limit"), $menuPerPage, $pageSelect);
			} elseif ($npaginas > 1 && count($this->_export) > 0) {
				if ($this->_recordsPerPage == 0) {
					$pag = '';
					$pageSelect = '';
				}

				$result2 =
						$this->_temp['table']->pagination($pag, $registoActual . ' ' . $this->__('to') . ' '
						. $registoFinal . ' ' . $this->__('of') . '  '
						. $this->_totalRecords, $menuPerPage, $pageSelect);
			} elseif ($npaginas < 2 && count($this->_export) > 0) {

				if ($this->_recordsPerPage == 0) {
					$pag = '';
					$pageSelect = '';
				}

				$result2 .= $this->_temp['table']->pagination('', $this->_totalRecords, $menuPerPage, $pageSelect);
			} elseif (count($this->_export) == 0) {
				if ($this->_recordsPerPage == 0) {
					$pag = '';
					$pageSelect = '';
				}
				$result2 .= $this->_temp['table']->pagination($pag, $this->_totalRecords, '', $pageSelect);
			}
		} else {
			return '';
		}

		return $result2;
	}

	public function deploy () {
		$content = preg_replace("#^<div id='{$this->getGridId()}'>|<\/div>$#", '', parent::deploy());

		$content = strtr($content, array(
			'{GRID_ID}' => $this->getGridId(),
				));

		$content .= $this->_buildPaginationAfterTable();

		$x = array(
			'gridName' => $this->_gridName,
			'content' => $content,
		);
		return $x;
	}

	public function addFormatterDir ($dir, $prefix) {
		$this->_formatter->addPrefixPath(trim($prefix, '_'), rtrim($dir, '/') . '/');
		return $this;
	}

	public function addTemplateDir ($dir, $prefix, $type) {
		if (!isset($this->_templates[$type])) {
			$this->_templates[$type] = new Zend_Loader_PluginLoader();
		}

		$this->_templates[$type]->addPrefixPath(trim($prefix, "_"), $dir, $type);
		return $this;
	}

	public function getNameGrid () {
		return $this->_gridName;
	}

	public function setNameGrid ($gridName) {
		$this->_gridName = $gridName;
	}

	protected function _buildGridTable ($grids) {
		return '<tbody>' . parent::_buildGridTable($grids) . '</tbody>';
	}

	protected function _pagination () {
		return strtr(parent::_pagination(), array('&nbsp;' => ''));
	}

}